package com.jieli.stream.dv.running2.camera;

import android.app.Activity;
import android.content.Context;
import android.view.SurfaceHolder;

import com.jieli.stream.dv.running2.util.Dbug;

public final class SurfaceViewCallback implements SurfaceHolder.Callback {
    private static final String TAG = SurfaceViewCallback.class.getSimpleName();
    private Context context;
    private CameraPresenter mPresenter;

    public SurfaceViewCallback(Context context) {
        this.context = context;
        mPresenter = new CameraPresenter();
    }

    public void setDeviceState(boolean isReady) {
        mPresenter.setDeviceState(isReady);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mPresenter.open();
        int rotation = ((Activity)context).getWindowManager().getDefaultDisplay().getRotation();
        //适配竖排固定角度
        mPresenter.setDisplayOrientation(rotation);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        if (mPresenter.isPreviewing()) {
            Dbug.i(TAG, "stop preview");
            mPresenter.stopPreview();
        }

        Dbug.i(TAG, "start preview");
        mPresenter.startPreview(arg0);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Dbug.i(TAG, "close camera");
        mPresenter.close();
    }
}